package GhostPhotography::Controller::Root;

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use Imager;
use File::Spec;
use Kaolabo;

use base 'Catalyst::Controller';

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config->{namespace} = '';

=head1 NAME

GhostPhotography::Controller::Root - Root Controller for GhostPhotography

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

=head2 default

=cut

sub default : Private {
    my ( $self, $c ) = @_;

    $c->stash->{ghost_material} = $c->config->{ghost_material};
    $c->stash->{template} = 'index.html';
    $c->forward($c->view('TT'));
}

sub convert : Local {
    my ( $self, $c ) = @_;

    $c->log->info($c->request->parameters->{photo_file});
    my $ghost = $c->req->param('ghost_type');

    my ( $ghost_material ) = grep{ $_->{name} eq $ghost } @{$c->config->{ghost_material}};

    my $upload = $c->req->upload('photo_file');
    if( $upload ){
        my $photo_id = time;
        $c->stash->{photo_id} = $photo_id;
        my $upload_file = File::Spec->catfile($c->config->{upload_temporary}, $photo_id.'.jpg');
        $upload->copy_to($upload_file);

        my $k = Kaolabo->new({
                                target_file  => $upload_file, 
                                uri          => $c->config->{kaolabo_webapi},
                                apikey       => $c->config->{apikey},
                            });
        
        $k->access;
        $k->effect_face({type=>'line',color=>'#000000'});

        my $img = $k->imager;

        my $request_content;

        my $w = $img->getwidth();
        my $h = $img->getheight();
        
        $img->rubthrough(
            src => do {
                my $scalefactor = 1;
                my $ghost = File::Spec->catfile($c->path_to,$c->config->{ghost_material_dir},$ghost_material->{file});
                my $tmp = Imager->new;
                $tmp->read( file => $ghost ) or die $tmp->errstr;

                my $sw = $w/$tmp->getwidth();
                my $sh = $h/$tmp->getheight();
                $scalefactor = ($sw>$sh)?$sw:$sh if( $sw > 1 || $sh > 1 );

                $tmp = $tmp->scale(scalefactor=>$scalefactor);
                my $alpha = $ghost_material->{alpha} || 0.3;

                $tmp = $tmp->convert(matrix =>[ [ 1, 0, 0, 0 ],
                                         [ 0, 1, 0, 0 ],
                                         [ 0, 0, 1, 0 ],
                                         [ 0, 0, 0, $alpha ],
                                     ]);
                $tmp;
            },
            tx => 0,
            ty => 0,
        );

        my $convert_photo = time.'.jpg';
        $c->stash->{convert_photo} = $convert_photo;
        my $convert_file = File::Spec->catfile($c->path_to,$c->config->{convert_temporary},$convert_photo);

        $k->write($convert_file);
    } 

    $c->stash->{template} = 'convert.html';
    $c->forward($c->view('TT'));
}

sub disspel : Local {
    my ( $self, $c ) = @_;

    $c->log->info($c->req->param('photo_id'));
    my $photo_id = $c->req->param('photo_id');

    if( $photo_id ){
        my $upload_file = File::Spec->catfile($c->config->{upload_temporary}, $photo_id.'.jpg');

        my $k = Kaolabo->new({
                                target_file  => $upload_file, 
                                uri          => $c->config->{kaolabo_webapi},
                                apikey       => $c->config->{apikey},
                            });
        
        $k->access;
        $k->effect_face({type=>'line',color=>'#000000'});

        my $convert_photo = time.'.jpg';
        $c->stash->{convert_photo} = $convert_photo;
        my $convert_file = File::Spec->catfile($c->path_to,$c->config->{convert_temporary},$convert_photo);

        $k->write($convert_file);
    } 

    $c->stash->{template} = 'dispel.html';
    $c->forward($c->view('TT'));

}



=head2 end

Attempt to render a view, if needed.

=cut 

sub end : ActionClass('RenderView') {}

=head1 AUTHOR

竹田 昭仁

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
